package com.booklove.service.book;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booklove.constant.DonateStateConstant;
import com.booklove.constant.JudgeStateConstant;
import com.booklove.context.BaseContext;
import com.booklove.domain.dto.DonatebookjudgeDTO;
import com.booklove.domain.po.Donatebook;
import com.booklove.domain.po.Donatebookjudge;
import com.booklove.domain.po.Donatebookjudgerecord;
import com.booklove.exception.BaseException;
import com.booklove.mapper.DonatebookMapper;
import com.booklove.mapper.DonatebookjudgeMapper;
import com.booklove.mapper.DonatebookjudgerecordMapper;
import com.booklove.service.voluntary.VoluntaryworkService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 捐赠书信息 服务实现类
 * </p>
 *
 * @author Z-ken
 * @since 2024/10/19 13:47:27
 */
@Service
public class DonatebookjudgeServiceImpl extends ServiceImpl<DonatebookjudgeMapper, Donatebookjudge> implements DonatebookjudgeService {


    @Autowired
    private DonatebookjudgeMapper donatebookjudgeMapper;
    @Autowired
    private DonatebookMapper donatebookMapper;
    @Autowired
    private DonatebookjudgerecordMapper donatebookjudgerecordMapper;

    @Autowired
    private VoluntaryworkService voluntaryworkService;


    @Override
    @Transactional // 事务方法
    public void judgeDonatebook(DonatebookjudgeDTO donatebookjudgeDTO) {

        // 此处不需要避免审核通过的不允许审核
        if (donatebookjudgeDTO.getResult() == null || donatebookjudgeDTO.getDonateBookId() == null) {
            throw new BaseException("传输了空的审核结果或者审核对象");
        }

        Integer result = donatebookjudgeDTO.getResult();

        Donatebookjudge donatebookjudge = donatebookjudgeMapper.selectById(donatebookjudgeDTO.getDonateBookId());

        if (result.equals(JudgeStateConstant.REJECT)) // 20203：审核失败
        {
            donatebookjudge.setJudgeState(JudgeStateConstant.REJECT);
            // 更新
            donatebookjudgeMapper.updateById(donatebookjudge);

        } else if (result.equals(JudgeStateConstant.ACCEPT)) {
            // 更新新数据
            donatebookjudge.setJudgeState(JudgeStateConstant.ACCEPT);

            // 审核通过-插入到表格中
            // 如果有审核完成过的，直接更新
            Donatebook donatebook = new Donatebook();
            if (donatebookjudge.getDonateBookId() != null) {
                BeanUtils.copyProperties(donatebookjudge, donatebook);
                donatebookMapper.updateById(donatebook);
            } else {// 如果没有，就进行插入
                BeanUtils.copyProperties(donatebookjudge, donatebook);
                donatebook.setDonateState(DonateStateConstant.WAITING);
                donatebookMapper.insert(donatebook);
                // 发布到志愿平台中
                voluntaryworkService.createCollectDonateBookWord(donatebook);
            }


            // 记录添加的审核通过项目
            donatebookjudge.setDonateBookId(donatebook.getId());
            donatebookjudgeMapper.updateById(donatebookjudge);


        } else if (result.equals(JudgeStateConstant.WAITING_JUDGE)) {
            throw new BaseException("不允许审核中的状态变换待审核中");
        }

        // 加入审核操作记录 -- 每一次操作都要记录
        Donatebookjudgerecord record = Donatebookjudgerecord.builder()
                .donateBookJudgeId(donatebookjudge.getId()) // 审核记录项目
                .judgeTime(LocalDateTime.now()) // 时间
                .judgeUserId(BaseContext.getCurrentId().intValue())// 审核人
                .judgeState(donatebookjudgeDTO.getResult())// 审核结果
                .build();

        donatebookjudgerecordMapper.insert(record);
    }
}
